/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.helloactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


/**
 * A minimal "Hello, World!" application.
 */
public class HelloActivity extends Activity {
	
    public HelloActivity() {
    }
    String player_black="player_black";
    String player_white="player_white";
    int playWithPhone=1;//play with phone
    int selectColor=0;//stand for black
    
    //to do ,has problem
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
    	
    	super.onActivityResult(requestCode, resultCode, data);
    	//Log.d("Debug","commmmmmmmmmme innnnnnnnnnnnnnnnnnnn");
    	switch(resultCode){
    		
    	case RESULT_OK:
    		Bundle bundle=data.getExtras();
    		playWithPhone=bundle.getInt("playWithPhone");
    		player_black=bundle.getString("player_black");
    		player_white=bundle.getString("player_white");
    		//Log.d("Debug","99999999999999999    "+playWithPhone+"        9999999999999999");
    		break;
    	default:
    		break;
    	}
    }
    
    /**
     * Called with the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the layout for this activity.  You can find it
        // in res/layout/hello_activity.xml
        //should call setContentView first,or core down
        setContentView(R.layout.hello_activity);
        Button btn_fast_start=(Button)findViewById(R.id.button_fast_game);
        btn_fast_start.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(HelloActivity.this, main_activity.class);
				Bundle bundle = new Bundle();
				bundle.putString("player_black", "player_black");
				bundle.putString("player_white", "player_white");
				bundle.putInt("playWithPhone", 1);
				//select black
				bundle.putInt("selectColor", 0);
				intent.putExtras(bundle);
				startActivity(intent);
			}
        	
        });
        
        Button btn_begin=(Button)findViewById(R.id.button_begin);
        btn_begin.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(HelloActivity.this, main_activity.class);
				Bundle bundle = new Bundle();
				bundle.putInt("playWithPhone", playWithPhone);

				//set the default name if the name is null or empty
		        if(null==player_black || player_black.equals("")){
		        	player_black="player_black";
		        }
		        if(null==player_white || player_white.equals("")){
		        	player_white="player_white";
		        }

		        Log.d("debug","player_black:"+player_black+"   player_white:"+player_white);
				if(1==playWithPhone && !player_white.equals("player_white") && player_black.equals("player_black")){

					bundle.putInt("selectColor", 1);
				}else{

					bundle.putInt("selectColor", 0);
				}
				bundle.putString("player_black", player_black);
				bundle.putString("player_white", player_white);
				intent.putExtras(bundle);
				startActivity(intent);
			}
        	
        });
        
        Button btn_quit=(Button)findViewById(R.id.button_quit);
        btn_quit.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
        	
        });
        
        Button btn_set_game=(Button)findViewById(R.id.button_set_game);
        btn_set_game.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putInt("playWithPhone", playWithPhone);
				bundle.putString("player_black", player_black);
				bundle.putString("player_white", player_white);
				intent.putExtras(bundle);
				intent.setClass(HelloActivity.this, SetGameActivity.class);
				//should not call startActivity(intent) for it will wait the return value
				startActivityForResult(intent,0);
			}
        	
        });
        
        Button btn_help=(Button)findViewById(R.id.button_help);
        btn_help.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(HelloActivity.this, help_activity.class);
				startActivity(intent);
			}
        	
        });
        
        
        //set the layout
        
        //
        //get the highth and width of screen
        //
        
        DisplayMetrics dm = new DisplayMetrics(); 
    	//int screenWidth=320;
    	int screenHeight=480;
        dm = this.getApplicationContext().getResources().getDisplayMetrics(); 
        //screenWidth = dm.widthPixels; 
        screenHeight = dm.heightPixels; 
        
        //LinearLayout ll=(LinearLayout)findViewById(R.id.ll_hello_activity);
        int textSize=screenHeight/16;
        btn_fast_start.setTextSize(textSize);
        btn_set_game.setTextSize(textSize);
        btn_help.setTextSize(textSize);
        btn_begin.setTextSize(textSize);
        btn_quit.setTextSize(textSize);
        
        int upTop=screenHeight/6;
        TextView view_empty=(TextView)findViewById(R.id.view_empty);
        view_empty.setHeight(upTop);

        
    }
}

